Skip to content

feat(security): add opt-in prompt injection heuristics - #52

Merged
ALikesToCode merged 2 commits into
integrate/gateway-waves-1-4from
feat/prompt-injection
Oct 9, 2026
Merged

ALikesToCode merged 2 commits into
integrate/gateway-waves-1-4from
feat/prompt-injection

Conversation

@ALikesToCode

Copy link
Copy Markdown
Owner

Summary

Adds opt-in prompt injection and jailbreak heuristics for managed requests. PROMPT_INJECTION_MODE is off by default. While it is off, requests, responses, headers and logs are unchanged and nothing is inspected.

  • Modes:
    • log dispatches normally and adds X-MultiLLM-Injection-Action: logged.
    • block returns 422 prompt_injection_suspected with X-MultiLLM-Injection-Action: blocked before compaction or generation.
  • Scoring: high-severity findings score 3 and medium ones 2. The default PROMPT_INJECTION_THRESHOLD is 3, and the valid range is 1 to 768. An invalid mode or threshold disables inspection and logs one warning, without the value.
  • Rules:
    • They look for instruction overrides, spoofed role delimiters, requests to exfiltrate credentials or system prompts, and bypass modes.
    • They inspect user and tool text only. System, developer and assistant roles, images, URLs and schemas are excluded.
    • Normalization covers compatibility Unicode, format characters, entities, percent escapes and literal Unicode escapes, in one pass, without changing the transmitted text.
  • Bounds: 1 MiB of text, 8,192 nodes and 256 findings per request. Every regex repetition is bounded. No remote classifier, base64 unpacking or custom regex is used.
  • Policy:
    • Key policy (prompt_injection_mode, prompt_injection_threshold) and route policy can only tighten the operator setting, and can't enable it when it is off.
    • A request body's prompt_injection field is ordinary content: it is neither interpreted nor removed.
  • Records: each decision is one content-free log line: rule IDs, counts, severity, score, truncation, mode and action. Matched text is never recorded. Decisions do not write dashboard audit rows or use D1.
  • Limits: heuristics can match quoted examples and can be evaded by obfuscation or other languages. A signal is evidence to review, not proof. This is documented.

Details are in docs/prompt-injection.md.

Wiring in this PR

  • Worker: roleplay turns are checked after validation and before compaction and generation (worker/roleplay/endpoint.mjs). The detector is loaded only when the mode is on.
  • Flask:
    • services/secret_firewall.py gains protect_managed_payload and an opt-in managed=True path on protect_payload.
    • The firewall's after-request hook sets the action header on success and on errors.

Coordinator edits in this PR

  • Exposed X-MultiLLM-Injection-Action in both CORS expose lists (route_helpers.py, worker/cors-policy.mjs).
  • Shared registrations:
    • tests/test_prompt_injection_worker.mjs in test:worker;
    • worker/prompt-injection-detection.mjs in the edge module inventory;
    • PROMPT_INJECTION_MODE and PROMPT_INJECTION_THRESHOLD in the Container env keys.
  • Round 2 of review removed two things:
    • the per-request dashboard audit rows, which were labelled setting_change;
    • a caller-supplied policy read from the request body.

Left for the Wave 5 integration

Registering register_prompt_injection(app, is_managed=..., route_policy=...) in app.py. It goes after retention and credential policy, and before identity, cache replay and admission, with a predicate that excludes raw forwarding.

Tests

  • tests/test_prompt_injection.py and tests/test_prompt_injection_worker.mjs cover:
    • each rule family;
    • normalization;
    • bounds and truncation;
    • tighten-only policy;
    • body fields left untouched;
    • log lines without content;
    • headers on success and error;
    • no inspection when off.
  • Full Python suite: 3,931 passed, 2 skipped. ruff, mypy and bandit are clean.
  • npm run test:worker: 1,486 passed. npm run test:integration: 7 passed.
  • test:deploy, free routes, client headers and the wrangler deploy --dry-run bundle all pass.

CI notes

  • GitHub python/node jobs: the dependency-audit steps fail on every branch, including main. fix(deps): clear the CI dependency audit advisories #42 fixes that.
  • Workers Builds: fails on this stack because the Wave 3 base adds the AdmissionCoordinator Durable Object migration, which wrangler versions upload cannot apply. This is expected until wrangler deploy runs on main.

This is part of Wave 5. It is based on integrate/gateway-waves-1-4, the Wave 4 integration branch. The README line comes with the Wave 5 integration PR.

Score managed requests against bounded prompt injection and jailbreak rules when PROMPT_INJECTION_MODE is log or block. Block mode refuses with 422 before dispatch; both modes report the decision in X-MultiLLM-Injection-Action and a content-free log line. Key and route policies can only tighten the operator policy, and request bodies cannot change it. With the mode unset, requests are not inspected.
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, add credits to your account and enable them for code reviews in your settings.

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Oct 9, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
❌ Deployment failed
View logs
multillm-proxy a532fae Oct 09 2026, 09:27 AM

@coderabbitai

coderabbitai Bot commented Oct 9, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 5301916b-4974-4098-82a9-7a4f7d86a9cf

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

…o feat/prompt-injection

# Conflicts:
#	package.json
@ALikesToCode
ALikesToCode merged commit 522f315 into integrate/gateway-waves-1-4 Oct 9, 2026
4 of 7 checks passed
@ALikesToCode
ALikesToCode deleted the feat/prompt-injection branch October 26, 2026 20:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant